<template>
    <div class="container">
        <div style="display: flex;width: 800px;">
            <div class="left"></div>
            <div class="right">
                <div style="text-align: center; font-size: 20px; margin-bottom: 20px; color: #333">欢迎使用</div>
                <el-form :model="form" :rules="rules" ref="formRef">
                    <el-form-item prop="username">
                        <el-input prefix-icon="el-icon-user" placeholder="请输入账号"
                                  v-model="form.displayName"></el-input>
                    </el-form-item>
                    <el-form-item label="头像">
                        <el-upload
                            class="avatar-uploader"
                            :action="$baseUrl + '/files/upload'"
                            list-type="picture"
                            :on-success="handleAvatarSuccess"
                            v-model="form.avatar"
                        >
                            <el-button type="primary" v-if="!form.avatar">上传图片</el-button>
                        </el-upload>
                    </el-form-item>
                    <el-form-item>
                        <el-button style="width: 100%; background-color: #333; border-color: #333; color: white"
                                   @click="login">注 册
                        </el-button>
                    </el-form-item>
                    <div style="display: flex; align-items: center">
                        <div style="flex: 1"></div>
                        <div style="flex: 1; text-align: right">
                            已有账号？请 <a href="/login">登录</a>
                        </div>
                    </div>
                </el-form>
            </div>
        </div>


    </div>
</template>

<script>
export default {
    name: "Register",
    data() {
        return {
            form: {},
            rules: {
                username: [
                    {required: true, message: '请输入账号', trigger: 'blur'},
                ],
                password: [
                    {required: true, message: '请输入密码', trigger: 'blur'},
                ]
            }
        }
    },
    created() {

    },
    methods: {
        login() {
            this.$refs['formRef'].validate((valid) => {
                if (valid) {
                    // 验证通过
                    this.$request.post('/register', this.form).then(res => {
                        if (res.code === '200') {
                            this.$message.success('注册成功')
                            location.href = '/login'
                        } else {
                            this.$message.error(res.msg)
                        }
                    })
                }
            })
        },
        handleAvatarSuccess(res, file) {
            this.form.avatar = res.data.data
        }
    }
}
</script>

<style scoped>
.container {
    height: 100vh;
    overflow: auto;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: rgb(225, 234, 250);
}

a {
    color: #2a60c9;
}

.left {
    flex: 1;
    background-image: url("@/assets/imgs/chat2.png");
    background-size: 100%;
    border-bottom-left-radius: 10px;
    border-top-left-radius: 10px;

}

.right {
    flex: 1;
    padding: 30px;
    background-color: white;
    border-bottom-right-radius: 10px;
    border-top-right-radius: 10px;
}
</style>